from datetime import datetime

from exts import db


class EmailCaptchaModel(db.Model):
    __tablename__ = 'email_captcha'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='自增id')
    email = db.Column(db.String(100), nullable=True, unique=True, comment='邮箱')
    captcha = db.Column(db.String(10), nullable=True, comment='验证码')
    create_time = db.Column(db.DateTime, default=datetime.now, comment='创建时间')


class UserModel(db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='自增id')
    username = db.Column(db.String(200), nullable=True, unique=True, comment='账号')
    password = db.Column(db.String(200), nullable=True, comment='密码')
    email = db.Column(db.String(100), nullable=True, unique=True, comment='邮箱')
    join_time = db.Column(db.DateTime, default=datetime.now, comment='加入时间')


class QuestionModel(db.Model):
    __tablename__ = 'question'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='自增id')
    title = db.Column(db.String(200), nullable=True, comment='标题')
    content = db.Column(db.TEXT, nullable=True, comment='内容')
    author_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    create_time = db.Column(db.DateTime, default=datetime.now, comment='创建时间')

    author = db.relationship('UserModel', backref='questions')


class AnswerModel(db.Model):
    __tablename__ = 'answer'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='自增id')
    content = db.Column(db.TEXT, nullable=True, comment='内容')
    create_time = db.Column(db.DateTime, default=datetime.now, comment='创建时间')
    question_id = db.Column(db.Integer, db.ForeignKey('question.id'))
    author_id = db.Column(db.Integer, db.ForeignKey('user.id'))

    question = db.relationship('QuestionModel', backref='answers')
    author = db.relationship('UserModel', backref='answers')
